package com.qzdsoft.erpcloud.controller.stock;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.com.CustomerSupplierContacts;
import com.qzdsoft.erpcloud.domain.stock.StockOutbound;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierService;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierStatementService;
import com.qzdsoft.erpcloud.service.pu.PurchaseOrderService;
import com.qzdsoft.erpcloud.service.stock.StockOutBoundService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.com.CustomerSupplierStatementInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseOrderEditInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseOrderInfo;
import com.qzdsoft.erpcloud.vo.stock.StockOutboundEditInfo;
import com.qzdsoft.erpcloud.vo.stock.StockOutboundInfo;
import com.qzdsoft.erpcloud.vo.stock.StockOutboundQueryInfo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.TypeInfo;
import com.qzdsoft.vo.pu.PurchaseOrderSourceType;
import com.qzdsoft.vo.pu.PurchaseOrderStatus;
import com.qzdsoft.vo.stock.StockOutType;

@Controller
@RequestMapping("stock/puoutbound")
public class StockPuOutBoundController extends BaseController
{
    private static final Logger logger = LoggerFactory.getLogger(StockPuOutBoundController.class);
    private static final String BASEPATH = "stock/puoutbound";
    
    @Autowired
    StockOutBoundService stockOutBoundService;
    @Autowired
    CustomerSupplierService customerSupplierService;
    @Autowired
    CustomerSupplierStatementService customerSupplierStatementService;
    @Autowired
    PurchaseOrderService purchaseOrderService;
    
    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status",status);
        mv.setViewName(BASEPATH+"/index");
        return mv;
    }
    /**
     * 按条件获取出库单
     * @param info
     * @return
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayTableResponse<StockOutboundInfo> list(StockOutboundQueryInfo info){
        if(info.isFirst()) {
            return LayTableResponse.nullDataPage();
        }else {
            info.setOutboundType(StockOutType.PUOUT_CODE); //设置为采购退货
            return stockOutBoundService.findPurchaseOrdersBy(info);
        }
    }
    
    /**
     * 转换视图
     * @param companyid
     * @param id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/{companyid}/{id}")
    public ModelAndView detail(@PathVariable("companyid")String companyid,@PathVariable("id")Integer id) {
        ModelAndView mv = new ModelAndView();
        
       if("view".equals(companyid)) {
           mv.setViewName(BASEPATH + "/view");
       }else if("add".equals(companyid)){
           mv.setViewName(BASEPATH+"/add");
       }else{
        logger.debug("查询销售出库单详情：companyid={},id={}",companyid,id);
        StockOutboundInfo order = stockOutBoundService.findOrdersBy(id);
        mv.addObject("order", order);
        List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status", status);
        mv.setViewName(BASEPATH + "/edit");
       }
        return mv;
    }
    
    /**
     * 选择来源采购订单页面跳转
     * @param companyid 公司id
     * @param id 采购订单id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/puedit/{companyid}/{id}")
    public ModelAndView puEdit(@PathVariable("companyid") String companyid, @PathVariable("id") Integer id)
    {
        logger.debug("查询采购退货单详情：companyid={},id={}",companyid,id);
        ModelAndView mv = new ModelAndView();
        PurchaseOrderInfo order = purchaseOrderService.findPurchaseOrderBy(id);
        mv.addObject("order", order);
        List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status", status);
        List<CustomerSupplierContacts> contacts = customerSupplierService.selectAll(order.getSupplierid()+"");
        mv.addObject("contacts",contacts);
        mv.addObject("types",PurchaseOrderSourceType.ALL);
        mv.setViewName(BASEPATH + "/puedit");
        return mv;
    }
    @Permission(validate=false)
    @RequestMapping("/addpu")
    public ModelAndView addPu()
    {
        ModelAndView mv = new ModelAndView();
        mv.setViewName(BASEPATH+"/pusource");
        return mv;
    }
    
    /**
     * 选择来源采购订单确认
     * @param info
     * @return
     */
    @ResponseBody
    @SysLog(description="保存采购退货来源单")
    @RequestMapping("/puselect")
    public Response<String> pusave(PurchaseOrderEditInfo info)
    {
        logger.debug("保存采购退货来源单:"+info);
        return stockOutBoundService.trasnlation(info);
    }
    
    
    /**
     * 编辑修改
     * @param info
     * @return
     */
    @ResponseBody
    @SysLog(description="编辑修改采购退货单")
    @RequestMapping("/edit")
    public Response<StockOutboundInfo> edit(StockOutboundEditInfo info){
        logger.debug("编辑修改采购退货单:"+info);
        return stockOutBoundService.edit(info);
    }
    
    /**
     * 新增保存
     * @param info
     * @return
     */
    @ResponseBody
    @SysLog(description="新增保存采购退货单")
    @RequestMapping("/save")
    public Response<String> save(StockOutbound info){
        logger.debug("新增保存采购退货单："+info);
        info.setOutboundType(StockOutType.PUOUT_CODE); //保存为采购退货
        return stockOutBoundService.save(info);
    }
    
    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @SysLog(description="删除采购退货单")
    @RequestMapping("/del")
    public Response<String> detele(Integer id) {
        logger.debug("删除采购退货单id:"+id);
        return stockOutBoundService.delete(id);
    }
    
    /**
     * 审核、弃审、关闭
     * @param id
     * @param status
     * @return
     */
    @ResponseBody
    @SysLog(description="审核、弃审、关闭采购退货单")
    @RequestMapping("/review")
    public Response<String> review(Integer id,Integer status) {
        logger.debug("审核、弃审、关闭采购退货单id："+id+" "+status);
        return stockOutBoundService.review(id,status);
    }
    
    /**
     * 供应商税率信息查询
     * @param supplierid
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/queryTax/{supplierid}")
    public TypeInfo queryTax(@PathVariable("supplierid")Integer supplierid ) {
        Integer companyid = 1;
        return customerSupplierService.selectTaxInfo(companyid, supplierid);
        
    }
    
    /**
     * 供应商结算方式查询
     * @param supplierid
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/querySettleMethod/{supplierid}")
    public List<CustomerSupplierStatementInfo> querySettleMethod(@PathVariable("supplierid")Integer supplierid) {
        //供应商结算方式
       Integer companyid = 1;
       List<CustomerSupplierStatementInfo> customerSupplierStatements = customerSupplierStatementService.selectBy(companyid, supplierid);
       return customerSupplierStatements;
    }

}
